package model.dba;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import model.Dias_Semana;
import model.Horario;

public class HorarioDAO extends JDBC {
	private static HorarioDAO horarioDAO;

	private HorarioDAO() {
		super();
	}

	public static HorarioDAO getInstance() {
		if (horarioDAO == null) {
			horarioDAO = new HorarioDAO();
		}
		return horarioDAO;
	}

	public int add(Horario horario) {

		try {
			String queryString = "INSERT INTO horarios( desde, hasta, dia)" + " VALUES( ?, ?, ?) ";

			preparedStatement = connection.prepareStatement(queryString);
			preparedStatement.setTimestamp(1, new Timestamp(horario.getDesde().getTimeInMillis()));
			preparedStatement.setTimestamp(2, new Timestamp(horario.getHasta().getTimeInMillis()));
			preparedStatement.setString(3, horario.getDia().toString());


			preparedStatement.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		return getLastId();

	}

	public boolean update(Horario horario) {
		boolean state = false;
		try {
			String queryString = "UPDATE horarios SET  desde=?, hasta=?, dia=? WHERE idhorario=?";
			preparedStatement.setTimestamp(1, new Timestamp(horario.getDesde().getTimeInMillis()));
			preparedStatement.setTimestamp(2, new Timestamp(horario.getHasta().getTimeInMillis()));
			preparedStatement.setString(3, horario.getDia().toString());
			preparedStatement.setInt(4, horario.getId());
			preparedStatement = connection.prepareStatement(queryString);

			state = preparedStatement.executeUpdate() != 0 ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			}

			catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();

			}
		}
		return state;
	}

	public boolean delete(Horario horario) {
		boolean state = false;
		try {
			String queryString = "DELETE FROM horarios WHERE idhorario=?";

			preparedStatement = connection.prepareStatement(queryString);
			preparedStatement.setInt(1, horario.getId());
			state = preparedStatement.executeUpdate() != 0 ? true : false;

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return state;

	}

	public Horario getHorario(int id) {

		Horario horario = new Horario();
		try {
			String queryString = "SELECT * FROM horarios where idhorario = '" + id + "';";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				horario = populate();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return horario;
	}

	public int getLastId() {
		int ret = 0;
		try {
			String queryString = "SELECT idhorario FROM horarios order by idhorario DESC LIMIT 1  ";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				ret = resultSet.getInt("idAlumno");

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		return ret;

	}

	public List<Horario> getAll() {
		List<Horario> horarioList = new ArrayList<Horario>();

		try {
			String queryString = "SELECT * FROM horarios  ;";

			preparedStatement = connection.prepareStatement(queryString);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				horarioList.add(populate());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (resultSet != null)
					resultSet.close();
				if (preparedStatement != null)
					preparedStatement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return horarioList;
	}

	private Horario populate() throws SQLException {

		Horario horario = new Horario();
		
		GregorianCalendar horaDesde = new GregorianCalendar();
		horaDesde.setTime(resultSet.getTimestamp("desde"));
		GregorianCalendar horaHasta = new GregorianCalendar();
		horaHasta.setTime(resultSet.getTimestamp("hasta"));
		
		horario.setDesde(horaDesde);
		horario.setDia(Dias_Semana.valueOf(resultSet.getString("dia").toString()));
		horario.setHasta(horaHasta);
		horario.setId((int) resultSet.getLong("idhorario"));

		return horario;

	}
}